using System;
using System.Collections;
using System.Html;
using System.Runtime.CompilerServices;
using Nsb.Classes;
using jQueryApi;

namespace WhirliGigEdit.Classes
{
    [IgnoreNamespace]
    public class Modifier : Actor
    {
        protected ModOptions Opts;
        private jQueryObject _opacity;

        public Modifier(ModOptions options)
        {
            Opts = options;
            Opts.IsColor = Opts.Prop != null && Opts.Prop.IndexOf("color") != -1;
        }

        [AlternateSignature]
        protected extern void Build();

        protected void Build(string classNames)
        {
            Glass(CloseEv);

            Element = jQuery.FromHtml("<div class='Modifier'/>")
                .AddClass("AboveHider")
                ;

            if (classNames != null)
                Element.AddClass(classNames);

            jQueryObject title = jQuery.FromHtml("<div class='Title'/>")
                .AppendTo(Element)
                .Html(Opts.Title)
                ;

            if (Opts.Html != null)
                Element.Append(Opts.Html.AddClass("Body"));
            else if (Opts.HtmlString != null)
                jQuery.FromHtml("<div class='Body'/>").AppendTo(Element).Html(Opts.HtmlString);
            if (Opts.Css != null)
                Element.CSS(Opts.Css);

            jQuery.FromElement(Document.Body)
                .Append(Element)
                ;

            Element.Plugin<Jewl>().Draggable(new Dictionary("handle", title));

            Element.Plugin<Jewl>().Position(new Dictionary("my", "center", "at", "center", "of", jQuery.Window));
        }

        public void OnChange(object v)
        {
            if (Opts.OnChange != null)
                Opts.OnChange(Opts, MixColor(v));
        }

        public void OnSave()
        {
            if (Opts.OnSave != null)
                Opts.OnSave(Opts);
        }

        public void CloseEv(jQueryEvent e)
        {
            Element.Remove();
        }

        public void OnProp(object v)
        {
            if (v != null)
            {
                OnChange(v);       
                OnSave();
            }
        }

        public object MixColor(object v)
        {
            if (Opts.IsColor)
            {
                string cc = (string) v;

                if (cc == "transparent")
                {
                    SetOpacity(0);
                    return cc;
                }
                if (cc == "none")
                {
                    SetOpacity(1);
                    return "";
                }
                if (_opacity != null)
                {
                    Color cx = Css.GetColor(cc);
                    cx.opacity = Number.ParseDouble(_opacity.GetValue());
                    if (cx.opacity == 0)
                    {
                        cx.opacity = 1;
                        SetOpacity(1);
                    }
                    return cx.ToString();
                }
            }
            return v;
        }

        public void OpacityEv(jQueryEvent e)
        {
            OnProp(Opts.ToStore[Opts.Prop]);
        }

        public void SetOpacity(double v)
        {
            if (_opacity != null)
                _opacity.Value((Math.Round(v*100)/100).ToString());
        }

        public jQueryObject OpacityEl()
        {
            _opacity = jQuery.FromHtml("<select class='SelOpacity'/>");
            for (int i=0; i<=100; i++)
            {
                jQuery.FromHtml("<option/>").Attribute("value", (i/100).ToString()).Html(i + "%").AppendTo(_opacity);
            }

            if (Opts.Prop != null && Opts.ToStore != null)
            {
                Color cx = Css.GetColor((string)Opts.ToStore[Opts.Prop]);
                SetOpacity(cx.opacity);
            }
            _opacity.Change(OpacityEv);
            return _opacity;
        }
    }

    public sealed class ModOptions
    {
        public string Title;
        public string Prop;
        public bool IsColor;
        public bool IsPgBackground;
        public jQueryObject Html;
        public string HtmlString;
        public jQueryObject ToChange;
        public Dictionary ToStore;
        public Action OnCancel;
        public Action<ModOptions> OnSave;
        public Action<ModOptions, object> OnChange;
        public Dictionary Css;
    }
}
